#!/bin/bash
echo -e "\033[31m ================================================= \033[0m"
echo -e "\033[31m 开始打包... \033[0m"
echo -e "\033[31m ================================================= \033[0m"

Project_Dir="./../BabaMeeting"
Project_Name="BabaMeeting"
Scheme="BabaMeeting"
SDK="iphoneos"
Configuration="Release"
BuildDir="./build"
ArchivePath="${BuildDir}/${Scheme}.xcarchive"
ExportOptionsPlist="./AppStoreExportOptions.plist"
ProvisioningProfilePath="./profile/MeetingBetaAdHoc.mobileprovision"
CodeSignIdentify="iPhone Distribution: 88 Meeting Limited (93F6B9Q7ZC)"
ExportPath="${BuildDir}"
ProjectPlistPath="${Project_Dir}/BabaMeeting/Info.plist"

echo -e "\033[31m ========删除上次打包文件 \033[0m"
rm -rf ${BuildDir}

echo -e "\033[31m ========设置版本号 \033[0m"
VersionString="2.3.1"
buildNumber="2310001"
/usr/libexec/PlistBuddy -c "Set :CFBundleVersion ${buildNumber}" "${ProjectPlistPath}"
/usr/libexec/PlistBuddy -c "Set :CFBundleShortVersionString ${VersionString}" "${ProjectPlistPath}"
echo -e "\033[31m ========当前打包版本号：${VersionString}(${buildNumber}) \033[0m"

echo -e "\033[31m ========开始归档 \033[0m"
ArchiveCMD="xcodebuild archive"
ArchiveCMD=${ArchiveCMD}" -workspace ${Project_Dir}/${Project_Name}.xcworkspace"
ArchiveCMD=${ArchiveCMD}" -scheme ${Scheme}"
ArchiveCMD=${ArchiveCMD}" -configuration ${Configuration}"
ArchiveCMD=${ArchiveCMD}" -sdk ${SDK}"
ArchiveCMD=${ArchiveCMD}" -archivePath ${ArchivePath}"
#ArchiveCMD=${ArchiveCMD}" CODE_SIGN_IDENTITY=\"${CodeSignIdentify}\""
#ArchiveCMD=${ArchiveCMD}" PROVISIONING_PROFILE=\"${mobileprovisionUUID}\""
#ArchiveCMD=${ArchiveCMD}" > /dev/null"
echo $ArchiveCMD
eval $ArchiveCMD

echo -e "\033[31m ========导出归档 \033[0m"
# 导出归档
ExportArchiveCMD="xcodebuild -exportArchive"
ExportArchiveCMD=${ExportArchiveCMD}" -archivePath ${ArchivePath}"
ExportArchiveCMD=${ExportArchiveCMD}" -exportOptionsPlist ${ExportOptionsPlist}"
ExportArchiveCMD=${ExportArchiveCMD}" -exportPath ${ExportPath}"
#ExportArchiveCMD=${ExportArchiveCMD}" CODE_SIGN_IDENTITY=\"${CodeSignIdentify}\""
#ExportArchiveCMD=${ExportArchiveCMD}" PROVISIONING_PROFILE=\"${mobileprovisionUUID}\""
#ExportArchiveCMD=${ExportArchiveCMD}" > /dev/null"
echo ${ExportArchiveCMD}
eval ${ExportArchiveCMD}


echo -e "\033[31m ========导出dsym文件 \033[0m"
cd ./build
cp -R ./${Project_Name}.xcarchive/dSYMs/${Project_Name}.app.dSYM ./
zip -r -9 "./${Project_Name}.app.dSYM.zip" "./${Project_Name}.app.dSYM"

IpaFile="./${Project_Name}.ipa"
echo "ipa:${IpaFile}"
if [ -e "${IpaFile}" ]; then
Account="88meeting.calendar@gmail.com"
Pwd="Stup1dApp1e!@#"
AppStoreCMD="/Applications/Xcode.app/Contents/Applications/Application\ Loader.app/Contents/Frameworks/ITunesSoftwareService.framework/Support/altool"
AppStoreCMD=${AppStoreCMD}" -f ${IpaFile}"
AppStoreCMD=${AppStoreCMD}" -u $Account"
AppStoreCMD=${AppStoreCMD}" -p $Pwd"
AppStoreCMD=${AppStoreCMD}" —output-format xml"
AppStoreValidateCMD=${AppStoreCMD}" --validate-app"
AppStoreUploadCMD=${AppStoreCMD}" —upload-app"
echo -e "\033[31m ========开始校验ipa包 \033[0m"
echo -e "\033[31m ========  ${AppStoreValidateCMD} ==== \033[0m"
eval $AppStoreValidateCMD
if [[ $? -eq 0 ]]; then
echo -e "\033[31m ========校验成功，开始上传app store \033[0m"
echo -e "\033[31m ========  ${AppStoreUploadCMD} ==== \033[0m"
eval $AppStoreUploadCMD
fi
else
echo "没有找到ipa文件:"
fi

